package com.qianli.user.application;

import com.fqgj.common.api.Response;
import com.google.common.collect.Lists;
import com.qianli.user.domain.enums.UserModelItemStatusEnum;
import com.qianli.user.domain.facatory.UserFeatureFactory;
import com.qianli.user.domain.model.UserFeatureModel;
import com.qianli.user.domain.model.feature.UserFeatureTag;
import com.qianli.user.ro.feature.FeatureTagRO;
import com.qianli.user.ro.feature.UserFeatureTagRO;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * name:用户特征信息
 * auth:ting.li@qianli-inc.com
 * date:2018/1/3 22:11
 * version: v1.0.0
 **/
@Service
public class UserFeatureApplication {

    @Autowired
    private UserFeatureFactory userFeatureFactory;

    public Response<Boolean> saveUserFeatureTags(List<UserFeatureTagRO> userFeatureTags) {
        String userCode = userFeatureTags.get(0).getUserCode();
        //加载用户标签
        UserFeatureModel model = new UserFeatureModel();
        model.setUserCode(userCode);
        userFeatureFactory.load(model);
        //compare
        UserFeatureModel newUm = new UserFeatureModel();
        newUm.setUserCode(userCode);
        convertROToModelWithFilter(userFeatureTags, model, newUm);
        userFeatureFactory.store(newUm);
        return Response.ok(true);
    }

    /**
     * ro convert
     *
     * @param userFeatureTags
     * @param model
     * @param newUm
     */
    private void convertROToModelWithFilter(List<UserFeatureTagRO> userFeatureTags, UserFeatureModel model,
                                            UserFeatureModel newUm) {
        //过滤已存在的标签
        if (!CollectionUtils.isEmpty(model.getUserFeatureTags())) {
            for (int i = 0; i < userFeatureTags.size(); i++) {
                for (UserFeatureTag uft : model.getUserFeatureTags()) {
                    Long tagId = userFeatureTags.get(i).getTagId();
                    if (null != tagId && tagId.equals(uft.getTagId())) {
                        userFeatureTags.remove(i);
                        i--;
                    }

                }
            }
        }
        convertROToModel(userFeatureTags, model, newUm);
    }

    /**
     * 移除标签
     *
     * @param userFeatureTags
     * @return
     */
    public Response<Boolean> removeUserFeatureTags(List<UserFeatureTagRO> userFeatureTags) {
        String userCode = userFeatureTags.get(0).getUserCode();
        //加载用户标签
        UserFeatureModel model = new UserFeatureModel();
        model.setUserCode(userFeatureTags.get(0).getUserCode());
        userFeatureFactory.load(model);
        //compare
        UserFeatureModel newUm = new UserFeatureModel();
        newUm.setUserCode(userCode);
        convertROToModel(userFeatureTags, model, newUm);
        userFeatureFactory.restore(newUm);
        return Response.ok(true);
    }

    /**
     * ro convert
     *
     * @param userFeatureTags
     * @param model
     * @param newUm
     */
    private void convertROToModel(List<UserFeatureTagRO> userFeatureTags, UserFeatureModel model,
                                  UserFeatureModel newUm) {
        List<UserFeatureTag> tags = Lists.newArrayList();
        for (UserFeatureTagRO tagRO : userFeatureTags) {
            UserFeatureTag tag = new UserFeatureTag();
            tag.setUserCode(model.getUserCode());
            tag.setTagId(tagRO.getTagId());
            tag.setName(tagRO.getName());
            tag.setUserTagType(tagRO.getType());
            tag.setItemStatus(UserModelItemStatusEnum.ADD.getType());
            tags.add(tag);
        }
        newUm.setUserFeatureTags(tags);
    }

    /**
     * 查询app所有标签
     *
     * @param appCode
     * @param biz
     * @return
     */
    public List<FeatureTagRO> queryAllFeatureTags(String appCode, Integer biz) {
        List<FeatureTagRO> tags = userFeatureFactory.queryAllFeatureTags(appCode, biz);
        return tags;
    }

    /**
     * 查询用户标签
     *
     * @param model
     * @return
     */
    public List<UserFeatureTagRO> load(UserFeatureModel model) {
        userFeatureFactory.load(model);
        List<UserFeatureTagRO> tagROs = Lists.newArrayList();
        List<UserFeatureTag> userFeatureTags = model.getUserFeatureTags();
        if (CollectionUtils.isEmpty(userFeatureTags)) {
            for (UserFeatureTag userFeatureTag : userFeatureTags) {
                UserFeatureTagRO tag = new UserFeatureTagRO();
                BeanUtils.copyProperties(userFeatureTag, tag);
                tagROs.add(tag);
            }
        }
        return tagROs;


    }
}
